using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using Game.XH;
using UnityEngine;

namespace Game.Component
{
	public class CopyingMachineItem : BaseItem
	{
		private Transform btnConfrim;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			btnConfrim = model.Find("BtnConfirm");
			btnConfrim.gameObject.layer = 15;
		}

		public override void ResetState()
		{
			base.ResetState();
			CheckState();
		}

		public void CheckState()
		{
			if (archiveData.createIndexs.Count >= base.itemData.createAmount)
			{
				model.Find("Error").SetObjectActive(true);
			}
			else
			{
				model.Find("Error").SetObjectActive(false);
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (archiveData.createIndexs.Count >= base.itemData.createAmount || childItems.Count < 1)
			{
				Singleton<AudioManager>.Instance.PlaySound("sound_error");
				return;
			}
			RaycastHit2D[] array = Physics2D.RaycastAll(touchEventData.curPos, Vector2.zero, 15f, 32768);
			for (int i = 0; i < array.Length; i++)
			{
				if (!(array[i].collider != null) || !(array[i].collider.transform == btnConfrim))
				{
					continue;
				}
				for (int j = 0; j < childItems.Count; j++)
				{
					if (CanCopy(childItems[j]))
					{
						BaseItem baseItem = Singleton<Scene>.Instance.CreateItem(childItems[j].itemData.id, model.Find("CreatePoint").position, 0);
						if (baseItem != null)
						{
							AddCreateItem(baseItem);
							baseItem.DropDown(new Vector3(baseItem.transform.position.x, base.transform.position.y - 1E-05f));
							Singleton<AudioManager>.Instance.PlaySound("sound_take_photo");
							Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_explode_1", baseItem.transform.position + new Vector3(0f, 0.5f, -0.0004f), 2f);
						}
						return;
					}
				}
				Singleton<AudioManager>.Instance.PlaySound("sound_error");
				break;
			}
		}

		public bool CanCopy(BaseItem item)
		{
			if (base.itemData.createIds == null || item == null)
			{
				return false;
			}
			for (int i = 0; i < base.itemData.createIds.Length; i++)
			{
				if (item.itemData.id == base.itemData.createIds[i])
				{
					return true;
				}
			}
			return false;
		}

		public override void AddCreateItem(BaseItem item)
		{
			base.AddCreateItem(item);
			CheckState();
		}

		public override void RemoveCreateItem(BaseItem item)
		{
			base.RemoveCreateItem(item);
			CheckState();
		}
	}
}
